﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using SmartHotel.MVC.Models;
using SmartHotel.MVC.MyFilter;
using System.Diagnostics;

namespace SmartHotel.MVC.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public IWebHostEnvironment WebHostEnvironment { get; }

        public HomeController(ILogger<HomeController> logger,IWebHostEnvironment webHostEnvironment)
        {
            _logger = logger;
            WebHostEnvironment = webHostEnvironment;
        }
        [LoginFilter]
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        /// <summary>
        /// 图片上传接口
        /// </summary>
        /// <returns></returns>
        public IActionResult TPSP()
        {
            //找到Request (浏览器请求)里的表单里的第一张图片
            var file = HttpContext.Request.Form.Files[0];
            //获取文件路径
            var dir = WebHostEnvironment.WebRootPath+"/imgs/";
            //使用guid 生成不重复的文件名
            var filename = Guid.NewGuid().ToString() + file.FileName;
            //把路径和名字合并到一起
            var path = Path.Combine(dir, filename);
            using (var stream = new FileStream(path,FileMode.CreateNew))
            {
                file.CopyTo(stream);
            }

            return Ok(new { msg="上传成功!",imgUrl = "/imgs/"+filename });
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        /// <summary>
        /// 上传文件的接口
        /// </summary>
        /// <returns></returns>

        public IActionResult Upload() {
            var file = HttpContext.Request.Form.Files[0];//取到上传的文件
            var dir = WebHostEnvironment.WebRootPath + "/imgs/";//图片文件夹的名字
            var filename = Guid.NewGuid()+ file.FileName;

            var filePath = Path.Combine(dir, filename);//合并文件夹和文件名称,形成一个路径
            using (var stream = new FileStream(filePath,FileMode.CreateNew))
            {
                file.CopyTo(stream);
            }
            return Ok(new { msg="上传成功!",imgUrl = "/imgs/"+filename});
        }

        public IActionResult htmlinput()
        {
            return View();
        }



        public IActionResult SC2207B()
        {
            var f = HttpContext.Request.Form.Files[0];//文件
            if (f.Length > 2 * 1024 * 1024) {
                return Ok(new { Msg="图片不得超过2兆!"});
            }

            var dir = WebHostEnvironment.WebRootPath+"/imgs/";//映射到wwwroot 这个文件夹
            if (!Directory.Exists(dir)) {
                Directory.CreateDirectory(dir);//不存在,新建一个文件夹
            }

            var filename = Guid.NewGuid()+ f.FileName;
            var path = Path.Combine(dir, filename);//把文件夹和文件名字合并成一个路径

            using (var stream = new FileStream(path,FileMode.CreateNew))
            {
                f.CopyTo(stream);
            }


            return Ok(new { Msg = "上传成功!",ImgUrl="/imgs/"+filename });
        }

    }
}
